/*
 * Filename:    AuditDispatchController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-10
 */
package com.paic.mhis.hcpms.audit.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.audit.biz.service.AuditOpinionService;
import com.paic.mhis.hcpms.audit.biz.service.IAuditDispatchService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchUserRequestDTO;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchUserResponseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditOpinionDTO;
import com.paic.mhis.hcpms.audit.dto.BatchSubmitResponseDTO;
import com.paic.mhis.hcpms.common.storage.biz.service.HcpmsStorageService;
import com.paic.mhis.hcpms.common.storage.dto.HcpmsStorageDTO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 该控制器主要是对审核岗 对生成的事件进行审核
 * 
 * @author EX-WANGKUI001
 * 
 */
@Controller
@RequestMapping("/auditFindDispatch/")
public class AuditFindDispatchController {

	@BizServicesProxy(beanName = "auditDispatchServiceImpl")
	private IAuditDispatchService auditDispatchService;

	@BizServicesProxy(beanName = "auditOpinionServiceImpl")
	private AuditOpinionService auditOpinionService;

	@BizServicesProxy(beanName = "hcpmsStorageService")
	private HcpmsStorageService hcpmsStorageService;

	/**
	 * 事件审核批量逻辑处理
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "problemBatchSubmit.do", method = RequestMethod.POST)
	@ResponseBody
	public BatchSubmitResponseDTO problemBatchSubmit(HttpServletRequest request)
			throws Exception {

		BatchSubmitResponseDTO messageBean = new BatchSubmitResponseDTO();

		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		String[] listCodes = request.getParameterValues("caseCodes");
		String opinion = request.getParameter("opinion");
		String opinionDesc = request.getParameter("opinionDesc");
		try {
			messageBean = this.auditDispatchService.processBatchSubmitInfo(
					listCodes, user, opinion, opinionDesc);
		} catch (Exception e) {
			messageBean.setFlag(false);
			messageBean.setMessage("操作失败!失败原因:" + e.getMessage());
		}

		return messageBean;

	}

	/**
	 * 初始化审核意见
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "processTempSave.do", method = RequestMethod.POST)
	@ResponseBody
	public AuditDispatchUserResponseDTO processTempSave(
			HttpServletRequest request) throws Exception {
		AuditDispatchUserResponseDTO responseBean = new AuditDispatchUserResponseDTO();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditDispatchUserRequestDTO vo = JSONObject.toJavaObject(
					transferJson, AuditDispatchUserRequestDTO.class);
			String type = vo.getType();
			vo.setOpertionUsername(user.getUsername());
			responseBean = this.auditDispatchService.processTempSave(vo, type,
					transferJson, user.getUsername());
			responseBean.setFlag(true);
		} catch (Exception e) {
			responseBean.setFlag(false);
		}
		return responseBean;
	}

	/**
	 * 加载事件意见说明根据事件编码
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadProblemOpinionInfo.do")
	@ResponseBody
	public AuditDispatchUserResponseDTO loadProblemOpinionInfo(@ModelAttribute
	AuditDispatchUserRequestDTO vo, HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");

		AuditOpinionDTO parameter = new AuditOpinionDTO();
		parameter.setProblemNo(vo.getProblemNo());
		parameter.setTaskType(AuditConstants.taskType_01);
		parameter.setOpinionType(AuditConstants.opinionType_1);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		AuditOpinionDTO returnBean = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
		tempObject.setBusinessId(vo.getProblemNo());
		tempObject.setModuleType(AuditConstants.sjModule);
		tempObject.setChildModuleType(AuditConstants.examine);
		tempObject.setBusinessType(AuditConstants.opinionType_2);
		tempObject.setUserId(user.getUsername());

		Object obj = this.hcpmsStorageService.getStorageDataMap(tempObject);
		if (obj != null) {
			AuditDispatchUserRequestDTO currentObj = JSONObject.toJavaObject(
					((JSONObject) obj), AuditDispatchUserRequestDTO.class);
			returnBean.setOpinionDesc(currentObj.getFirstOpinionDesc());
		}
		AuditDispatchUserResponseDTO bean = new AuditDispatchUserResponseDTO();
		bean.setTreatmentOpinion(returnBean);

		return bean;
	}

	/**
	 * 审核岗位 审核是否调查 选择不同意的确定按钮
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "auditConfirmProcess.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> auditConfirmProcess(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditDispatchUserRequestDTO vo = JSONObject.toJavaObject(
					transferJson, AuditDispatchUserRequestDTO.class);

			String username = user.getUsername();
			vo.setOpertionUsername(username);

			result = this.auditDispatchService.aduitSuveryDisagreeConfirm(vo,
					transferJson, user, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 审核岗 在分配任务的时候 选择了经办刚A 和经办刚B 后点击确定按钮或者随机分配后台处理逻辑
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "auditProblemConfirmDispatch.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> auditProblemConfirmDispatch(
			HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditDispatchUserRequestDTO vo = JSONObject.toJavaObject(
					transferJson, AuditDispatchUserRequestDTO.class);

			String username = user.getUsername();
			vo.setOpertionUsername(username);

			this.auditDispatchService.auditDispatchTaskSurvey(vo, user,
					transferJson);
			result.put("state", AuditConstants.FLAG_TURE);
			result.put("msg", AuditConstants.DISTRIBUT_SUCCESS);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

}
